<?php

/***  DOCUMENTATION LAYER

Klenwell DBO Unit Test

Last Update: Aug 2007
  
DESCRIPTION
  this file provides a template for other simple unit tests.
  
NOTES
  $test_root dynamically sets root in order to keep tests working after
  they are moved to the __REPOSITORY directory
    
______________________________________________________________________________*/

$_tx0 = microtime();
$test_root = ( strpos(dirname(__FILE__), 'repository') === false ) ? '' : '../';


// Test Name
$test_name = 'kdbo test';

// Include Files
require_once($test_root . 'class.kdbo.php');
#require_once(dirname($_SERVER['DOCUMENT_ROOT']) . '/__DEV/kdbo.mysql_cred.inc.php');   // my db connection info is here

// Configure Test
$debug = 1;

// styling functions
function p1($bg='#fff') { print "<pre style='padding:1em; background:{$bg}; border:4px solid #ccc;'>"; }
function p2() { print '</pre>'; }

$DB['user'] = $DB['pass'] = 'root';
$DB['host'] = 'localhost';

// *** Test Procedure

// Connect
p1('#fff');

// cx KDBO object
if ( $Dbo = new Kdbo('mysql', $debug=1, $adopath='../adodb_lite/adodb.inc.php') ) $Dbo->print_d("kdbo object loaded");


// connect
if ( $Dbo->connect($db_name='kdbo', $DB['user'], $DB['pass'], $DB['host']) )
{
  $Dbo->print_d("connected to db [$db_name]", 'green');
}
else
{
  $Dbo->print_d("failed to connect to db [$db_name]", 'red');
}
p2();


// recreate table
p1('#eef');
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS `test` 
(
	/* Columns */
  `uid` int(11) NOT NULL auto_increment,
  `varchar` varchar(40) NOT NULL default '',
  `int` int(11) NOT NULL default '0',
  `tiny_int` tinyint(4) NOT NULL default '0',
  `double` double(10,2) NOT NULL default '0.00',
  `text` text,
  `datetime` datetime NOT NULL default '0000-00-00 00:00:00',
  `uts` int(11) NOT NULL default '0',	

	/* Indices */
  PRIMARY KEY  (`uid`),
	INDEX (`varchar`),
  UNIQUE (`uts`)
) 
TYPE=MyISAM 
AUTO_INCREMENT=1 ;
SQL;

$Dbo->print_d('recreating db and table');
$Dbo->exec('DROP DATABASE IF EXISTS `kdbo`');
$Dbo->exec('CREATE DATABASE IF NOT EXISTS `kdbo` ;');
$Dbo->exec('USE `kdbo`');
$Dbo->exec('DROP TABLE IF EXISTS `test`');
if ( $Dbo->exec($sql) ) $Dbo->print_d('db and table recreated', 'green');
p2();



// simple insert
$table = 'test';

p1('#eff');
$DATA_BIND = array
(
  md5($time = time()),
  mt_rand(1,100000),
  mt_rand(1,$time) / 14.7,
  date('Y-m-d H:i:s'),
  $time
);

$Dbo->print_d("test insert [".print_r($DATA_BIND,1)."] into table [$table]", '#999');
$sql = "INSERT INTO $table ( `varchar`, `int`, `double`, `datetime`, `uts` ) VALUES ( ?,?,?,?,? )";

if ( !$RecordSet = $Dbo->exec($sql, $DATA_BIND) )
{
  trigger_error("query [$sql] failed", E_USER_WARNING);
}
else
{
  $insert_id = $Dbo->get_insert_id();
  $Dbo->print_d('data successfully inserted - insert id: ' . $insert_id, 'green');
  $Dbo->print_d('Record Set Object: ' . $Dbo->print_r($RecordSet), '#333');
} 
p2();

// simple update
p1('#eef');
$Dbo->print_d("simple update", '#999');
$DATA_BIND = array( 'updated', $time + 1, $insert_id );
$Dbo->print_d("test update [".print_r($DATA_BIND,1)."] in table [$table]", '#999');
$sql = "UPDATE $table SET `varchar`=?, `uts`=? WHERE `uid`=?";
$RecordSet = null;
if ( !$RecordSet = $Dbo->exec($sql, $DATA_BIND) )
  trigger_error("query [$sql] failed", E_USER_WARNING);
else
{
  $Dbo->print_d('update succeded', 'green');
  $Dbo->print_d('Record Set Object: ' . $Dbo->print_r($RecordSet), '#333');
} 
p2();


// delete some
if ( mt_rand(1,2) == 1 )
{
p1('#fee');
  $Dbo->print_d("deleting entry", '#999');
  $sql = "DELETE FROM $table WHERE `uid`=" . $insert_id;
  $RecordSet = null;
  if ( !$RecordSet = $Dbo->exec($sql, false) )
    trigger_error("query [$sql] failed", E_USER_WARNING);
  else
  {
    $Dbo->print_d('delete succeeded', 'green');
    $Dbo->print_d('Record Set Object: ' . $Dbo->print_r($RecordSet), '#333');
  }
p2();
}

// test insert array
p1('#efe');
$Dbo->print_d('testing insert_array($table, $ARRAY, $check_cols=0) method', '#999');
$ARRAY = array
(
  'varchar' => md5($time = time()),
  'int' => mt_rand(1,100000),
  'text' => 'a test of insert_array method',
  'datetime' => date('Y-m-d H:i:s'),
  'uts' => $time + 2
);
if ( !$insert_id = $Dbo->insert_array($table, $ARRAY, $check_cols=0) )
{
  $Dbo->print_d('insert_array failed', 'red');
}
else
{
  $Dbo->print_d('insert_array succeeded - insert id: ' . $insert_id, 'green');
}
p2();


// test insert array
p1('#fed');
$Dbo->print_d('testing update_array($table, $ARRAY, $key_col, $key_val, $check_cols=0)', '#999');
$Dbo->print_d('note: return value should be checked against FALSE [=== FALSE / !== FALSE], not empty, as 0 could be returned after successful query');
$ARRAY = array
(
  'varchar' => 'this has been updated',
  'text' => "a test of update_array method -- updated uid #$insert_id",
  'datetime' => date('Y-m-d H:i:s'),
);
$rows_updated = $Dbo->update_array($table, $ARRAY, 'uid', $insert_id, $check_cols=0);
if ( $rows_updated === FALSE )
{
  $Dbo->print_d('update_array failed', 'red');
}
else
{
  $Dbo->print_d("update_array succeeded : $rows_updated rows updated", 'green');
}
p2();


// test error
p1('#f99');
$Dbo->print_d('testing insert_array error', '#333');
$ARRAY = array
(
  'varchar' => md5($time = time()),
  'text' => 'a test of insert_array error',
  'datetime' => date('Y-m-d H:i:s'),
  'uts' => $time + 2,
  'error' => 'this col does not exist',
  
);
if ( !$insert_id = $Dbo->insert_array($table, $ARRAY, $check_cols=0) )
{
  $Dbo->print_d('insert_array failed', 'red');
}
else
{
  $Dbo->print_d('insert_array succeeded - insert id: ' . $insert_id, 'green');
}
p2();


$Dbo->print_d("<b>{$Dbo->Ado->query_count} queries completed in {$Dbo->Ado->query_time_total} s</b>", 'purple');

p1('#ccc');
$Dbo->print_d($Dbo->print_r($Dbo), '#006');
p2();


// Footer
$_txn = microtime();
$stx = number_format(((substr($_txn,0,9)) + (substr($_txn,-10)) - (substr($_tx0,0,9)) - (substr($_tx0,-10))),4); 
echo "<h6>test '$test_name' complete in $stx s</h6>";
?>
